


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Automatic Mixed Precision package - torch.cuda.amp &mdash; PyTorch master documentation</title>
  

  
  
  
  
    <link rel="canonical" href="https://pytorch.org/docs/stable/amp.html"/>
  

  

  
  
    

  

  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="_static/css/jit.css" type="text/css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Distributed communication package - torch.distributed" href="distributed.html" />
    <link rel="prev" title="torch.cuda" href="cuda.html" /> 

  
  <script src="_static/js/modernizr.min.js"></script>

  <!-- Preload the theme fonts -->

<link rel="preload" href="_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">

<!-- Preload the katex fonts -->

<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
</head>

<div class="container-fluid header-holder tutorials-header" id="header-holder">
  <div class="container">
    <div class="header-container">
      <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>

      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <div class="ecosystem-dropdown">
              <a id="dropdownMenuButton" data-toggle="ecosystem-dropdown">
                Ecosystem
              </a>
              <div class="ecosystem-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/hub"">
                  <span class=dropdown-title>Models (Beta)</span>
                  <p>Discover, publish, and reuse pre-trained models</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/ecosystem">
                  <span class=dropdown-title>Tools & Libraries</span>
                  <p>Explore the ecosystem of tools and libraries</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <div class="resources-dropdown">
              <a id="resourcesDropdownButton" data-toggle="resources-dropdown">
                Resources
              </a>
              <div class="resources-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/resources"">
                  <span class=dropdown-title>Developer Resources</span>
                  <p>Find resources and get questions answered</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/features">
                  <span class=dropdown-title>About</span>
                  <p>Learn about PyTorch’s features and capabilities</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>

      <a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
    </div>

  </div>
</div>


<body class="pytorch-body">

   

    

    <div class="table-of-contents-link-wrapper">
      <span>Table of Contents</span>
      <a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
    </div>

    <nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
      <div class="pytorch-side-scroll">
        <div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          <div class="pytorch-left-menu-search">
            

            
              
              
                <div class="version">
                  master (1.5.0 )
                </div>
              
            

            


  


<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search Docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

            
          </div>

          
<div>
  <a style="color:#F05732" href="https://pytorch.org/docs/stable/amp.html">
    You are viewing unstable developer preview docs.
    Click here to view docs for latest stable release.
  </a>
</div>

            
            
              
            
            
              <p class="caption"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/amp_examples.html">Automatic Mixed Precision examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/autograd.html">Autograd mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/broadcasting.html">Broadcasting semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cpu_threading_torchscript_inference.html">CPU threading and TorchScript inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/ddp.html">Distributed Data Parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/cppdocs/">C++ API</a></li>
<li class="toctree-l1"><a class="reference internal" href="packages.html">Javadoc</a></li>
</ul>
<p class="caption"><span class="caption-text">Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="cuda.html">torch.cuda</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">torch.cuda.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="hub.html">torch.hub</a></li>
<li class="toctree-l1"><a class="reference internal" href="jit.html">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.init.html">torch.nn.init</a></li>
<li class="toctree-l1"><a class="reference internal" href="onnx.html">torch.onnx</a></li>
<li class="toctree-l1"><a class="reference internal" href="optim.html">torch.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="rpc/index.html">Distributed RPC Framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="random.html">torch.random</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse.html">torch.sparse</a></li>
<li class="toctree-l1"><a class="reference internal" href="storage.html">torch.Storage</a></li>
<li class="toctree-l1"><a class="reference internal" href="bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensorboard.html">torch.utils.tensorboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="type_info.html">Type Info</a></li>
<li class="toctree-l1"><a class="reference internal" href="named_tensor.html">Named Tensors</a></li>
<li class="toctree-l1"><a class="reference internal" href="name_inference.html">Named Tensors operator coverage</a></li>
<li class="toctree-l1"><a class="reference internal" href="__config__.html">torch.__config__</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/audio">torchaudio</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/text">torchtext</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/elastic/">TorchElastic</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/serve">TorchServe</a></li>
<li class="toctree-l1"><a class="reference external" href="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
<p class="caption"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/governance.html">PyTorch Governance</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Persons of Interest</a></li>
</ul>

            
          

        </div>
      </div>
    </nav>

    <div class="pytorch-container">
      <div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
        <div class="pytorch-breadcrumbs-wrapper">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="pytorch-breadcrumbs">
    
      <li>
        <a href="index.html">
          
            Docs
          
        </a> &gt;
      </li>

        
      <li>Automatic Mixed Precision package - torch.cuda.amp</li>
    
    
      <li class="pytorch-breadcrumbs-aside">
        
            
            <a href="_sources/amp.rst.txt" rel="nofollow"><img src="_static/images/view-page-source-icon.svg"></a>
          
        
      </li>
    
  </ul>

  
</div>
        </div>

        <div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
          Shortcuts
        </div>
      </div>

      <section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
        <div class="pytorch-content-left">

        
          
          <div class="rst-content">
          
            <div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
             <article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
              
  <div class="section" id="module-torch.cuda.amp">
<span id="automatic-mixed-precision-package-torch-cuda-amp"></span><h1>Automatic Mixed Precision package - torch.cuda.amp<a class="headerlink" href="#module-torch.cuda.amp" title="Permalink to this headline">¶</a></h1>
<p><code class="docutils literal notranslate"><span class="pre">torch.cuda.amp</span></code> provides convenience methods for running networks with mixed precision,
where some operations use the <code class="docutils literal notranslate"><span class="pre">torch.float32</span></code> (<code class="docutils literal notranslate"><span class="pre">float</span></code>) datatype and other operations
use <code class="docutils literal notranslate"><span class="pre">torch.float16</span></code> (<code class="docutils literal notranslate"><span class="pre">half</span></code>). Some operations, like linear layers and convolutions,
are much faster in <code class="docutils literal notranslate"><span class="pre">float16</span></code>. Other operations, like reductions, often require the dynamic
range of <code class="docutils literal notranslate"><span class="pre">float32</span></code>. Networks running in mixed precision try to match each operation to its appropriate datatype.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><a class="reference internal" href="#torch.cuda.amp.GradScaler" title="torch.cuda.amp.GradScaler"><code class="xref py py-class docutils literal notranslate"><span class="pre">torch.cuda.amp.GradScaler</span></code></a> is not a complete implementation of automatic mixed precision.
<a class="reference internal" href="#torch.cuda.amp.GradScaler" title="torch.cuda.amp.GradScaler"><code class="xref py py-class docutils literal notranslate"><span class="pre">GradScaler</span></code></a> is only useful if you manually run regions of your model in <code class="docutils literal notranslate"><span class="pre">float16</span></code>.
If you aren’t sure how to choose op precision manually, the master branch and nightly pip/conda
builds include a context manager that chooses op precision automatically wherever it’s enabled.
See the <a class="reference external" href="https://pytorch.org/docs/master/amp.html">master documentation</a> for details.</p>
</div>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#gradient-scaling" id="id2">Gradient Scaling</a></p></li>
</ul>
</div>
<div class="section" id="gradient-scaling">
<span id="id1"></span><h2><a class="toc-backref" href="#id2">Gradient Scaling</a><a class="headerlink" href="#gradient-scaling" title="Permalink to this headline">¶</a></h2>
<p>When training a network with mixed precision, if the forward pass for a particular op has
<code class="docutils literal notranslate"><span class="pre">torch.float16</span></code> inputs, the backward pass for that op will produce <code class="docutils literal notranslate"><span class="pre">torch.float16</span></code> gradients.
Gradient values with small magnitudes may not be representable in <code class="docutils literal notranslate"><span class="pre">torch.float16</span></code>.
These values will flush to zero (“underflow”), so the update for the corresponding parameters will be lost.</p>
<p>To prevent underflow, “gradient scaling” multiplies the network’s loss(es) by a scale factor and
invokes a backward pass on the scaled loss(es).  Gradients flowing backward through the network are
then scaled by the same factor.  In other words, gradient values have a larger magnitude,
so they don’t flush to zero.</p>
<p>The parameters’ gradients (<code class="docutils literal notranslate"><span class="pre">.grad</span></code> attributes) should be unscaled before the optimizer uses them
to update the parameters, so the scale factor does not interfere with the learning rate.</p>
<dl class="class">
<dt id="torch.cuda.amp.GradScaler">
<em class="property">class </em><code class="sig-prename descclassname">torch.cuda.amp.</code><code class="sig-name descname">GradScaler</code><span class="sig-paren">(</span><em class="sig-param">init_scale=65536.0</em>, <em class="sig-param">growth_factor=2.0</em>, <em class="sig-param">backoff_factor=0.5</em>, <em class="sig-param">growth_interval=2000</em>, <em class="sig-param">enabled=True</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler" title="Permalink to this definition">¶</a></dt>
<dd><p>An instance <code class="docutils literal notranslate"><span class="pre">scaler</span></code> of <a class="reference internal" href="#torch.cuda.amp.GradScaler" title="torch.cuda.amp.GradScaler"><code class="xref py py-class docutils literal notranslate"><span class="pre">GradScaler</span></code></a> helps perform the steps of gradient scaling
conveniently.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">scaler.scale(loss)</span></code> multiplies a given loss by <code class="docutils literal notranslate"><span class="pre">scaler</span></code>’s current scale factor.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">scaler.step(optimizer)</span></code> safely unscales gradients and calls <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">scaler.update()</span></code> updates <code class="docutils literal notranslate"><span class="pre">scaler</span></code>’s scale factor.</p></li>
</ul>
<p>Typical use:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Creates a GradScaler once at the beginning of training.</span>
<span class="n">scaler</span> <span class="o">=</span> <span class="n">GradScaler</span><span class="p">()</span>

<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="n">epochs</span><span class="p">:</span>
    <span class="k">for</span> <span class="nb">input</span><span class="p">,</span> <span class="n">target</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
        <span class="n">optimizer</span><span class="o">.</span><span class="n">zero_grad</span><span class="p">()</span>
        <span class="n">output</span> <span class="o">=</span> <span class="n">model</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
        <span class="n">loss</span> <span class="o">=</span> <span class="n">loss_fn</span><span class="p">(</span><span class="n">output</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span>

        <span class="c1"># Scales the loss, and calls backward() on the scaled loss to create scaled gradients.</span>
        <span class="n">scaler</span><span class="o">.</span><span class="n">scale</span><span class="p">(</span><span class="n">loss</span><span class="p">)</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>

        <span class="c1"># scaler.step() first unscales the gradients of the optimizer&#39;s assigned params.</span>
        <span class="c1"># If these gradients do not contain infs or NaNs, optimizer.step() is then called,</span>
        <span class="c1"># otherwise, optimizer.step() is skipped.</span>
        <span class="n">scaler</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">optimizer</span><span class="p">)</span>

        <span class="c1"># Updates the scale for next iteration.</span>
        <span class="n">scaler</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
</pre></div>
</div>
<p>See the <a class="reference internal" href="notes/amp_examples.html#gradient-scaling-examples"><span class="std std-ref">Gradient Scaling Examples</span></a> for usage in more complex cases like
gradient clipping, gradient penalty, and multiple losses/optimizers.</p>
<p><code class="docutils literal notranslate"><span class="pre">scaler</span></code> dynamically estimates the scale factor each iteration.  To minimize gradient underflow,
a large scale factor should be used.  However, <code class="docutils literal notranslate"><span class="pre">torch.float16</span></code> values can “overflow” (become inf or NaN) if
the scale factor is too large.  Therefore, the optimal scale factor is the largest factor that can be used
without incurring inf or NaN gradient values.
<code class="docutils literal notranslate"><span class="pre">scaler</span></code> approximates the optimal scale factor over time by checking the gradients for infs and NaNs during every
<code class="docutils literal notranslate"><span class="pre">scaler.step(optimizer)</span></code> (or optional separate <code class="docutils literal notranslate"><span class="pre">scaler.unscale_(optimizer)</span></code>, see <a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a>).</p>
<ul class="simple">
<li><p>If infs/NaNs are found, <code class="docutils literal notranslate"><span class="pre">scaler.step(optimizer)</span></code> skips the underlying <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code> (so the params
themselves remain uncorrupted) and <code class="docutils literal notranslate"><span class="pre">update()</span></code> multiplies the scale by <code class="docutils literal notranslate"><span class="pre">backoff_factor</span></code>.</p></li>
<li><p>If no infs/NaNs are found, <code class="docutils literal notranslate"><span class="pre">scaler.step(optimizer)</span></code> runs the underlying <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code> as usual.
If <code class="docutils literal notranslate"><span class="pre">growth_interval</span></code> unskipped iterations occur consecutively, <code class="docutils literal notranslate"><span class="pre">update()</span></code> multiplies the scale by
<code class="docutils literal notranslate"><span class="pre">growth_factor</span></code>.</p></li>
</ul>
<p>The scale factor often causes infs/NaNs to appear in gradients for the first few iterations as its
value calibrates.  <code class="docutils literal notranslate"><span class="pre">scaler.step</span></code> will skip the underlying <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code> for these
iterations.  After that, step skipping should occur rarely (once every few hundred or thousand iterations).</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>init_scale</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a><em>, </em><em>optional</em><em>, </em><em>default=2.**16</em>) – Initial scale factor.</p></li>
<li><p><strong>growth_factor</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a><em>, </em><em>optional</em><em>, </em><em>default=2.0</em>) – Factor by which the scale is multiplied during
<a class="reference internal" href="#torch.cuda.amp.GradScaler.update" title="torch.cuda.amp.GradScaler.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a> if no inf/NaN gradients occur for <code class="docutils literal notranslate"><span class="pre">growth_factor</span></code> consecutive iterations.</p></li>
<li><p><strong>backoff_factor</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a><em>, </em><em>optional</em><em>, </em><em>default=0.5</em>) – Factor by which the scale is multiplied during
<a class="reference internal" href="#torch.cuda.amp.GradScaler.update" title="torch.cuda.amp.GradScaler.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a> if inf/NaN gradients occur in an iteration.</p></li>
<li><p><strong>growth_interval</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.8)"><em>int</em></a><em>, </em><em>optional</em><em>, </em><em>default=2000</em>) – Number of consecutive iterations without inf/NaN gradients
that must occur for the scale to be multiplied by <code class="docutils literal notranslate"><span class="pre">growth_factor</span></code>.</p></li>
<li><p><strong>enabled</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.8)"><em>bool</em></a><em>, </em><em>optional</em><em>, </em><em>default=True</em>) – If <code class="docutils literal notranslate"><span class="pre">False</span></code>, disables gradient scaling. <a class="reference internal" href="#torch.cuda.amp.GradScaler.step" title="torch.cuda.amp.GradScaler.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">step()</span></code></a> simply
invokes the underlying <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code>, and other methods become no-ops.</p></li>
</ul>
</dd>
</dl>
<dl class="method">
<dt id="torch.cuda.amp.GradScaler.get_backoff_factor">
<code class="sig-name descname">get_backoff_factor</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.get_backoff_factor"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.get_backoff_factor" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a Python float containing the scale backoff factor.</p>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.get_growth_factor">
<code class="sig-name descname">get_growth_factor</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.get_growth_factor"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.get_growth_factor" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a Python float containing the scale growth factor.</p>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.get_growth_interval">
<code class="sig-name descname">get_growth_interval</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.get_growth_interval"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.get_growth_interval" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a Python int containing the growth interval.</p>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.get_scale">
<code class="sig-name descname">get_scale</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.get_scale"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.get_scale" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a Python float containing the current scale, or 1.0 if scaling is disabled.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><a class="reference internal" href="#torch.cuda.amp.GradScaler.get_scale" title="torch.cuda.amp.GradScaler.get_scale"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_scale()</span></code></a> incurs a CPU-GPU sync.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.is_enabled">
<code class="sig-name descname">is_enabled</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.is_enabled"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.is_enabled" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a bool indicating whether this instance is enabled.</p>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.load_state_dict">
<code class="sig-name descname">load_state_dict</code><span class="sig-paren">(</span><em class="sig-param">state_dict</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.load_state_dict"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.load_state_dict" title="Permalink to this definition">¶</a></dt>
<dd><p>Loads the scaler state.  If this instance is disabled, <a class="reference internal" href="#torch.cuda.amp.GradScaler.load_state_dict" title="torch.cuda.amp.GradScaler.load_state_dict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_state_dict()</span></code></a> is a no-op.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>state_dict</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.8)"><em>dict</em></a>) – scaler state.  Should be an object returned from a call to <a class="reference internal" href="#torch.cuda.amp.GradScaler.state_dict" title="torch.cuda.amp.GradScaler.state_dict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">state_dict()</span></code></a>.</p>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.scale">
<code class="sig-name descname">scale</code><span class="sig-paren">(</span><em class="sig-param">outputs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.scale"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.scale" title="Permalink to this definition">¶</a></dt>
<dd><p>Multiplies (‘scales’) a tensor or list of tensors by the scale factor.</p>
<p>Returns scaled outputs.  If this instance of <a class="reference internal" href="#torch.cuda.amp.GradScaler" title="torch.cuda.amp.GradScaler"><code class="xref py py-class docutils literal notranslate"><span class="pre">GradScaler</span></code></a> is not enabled, outputs are returned
unmodified.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>outputs</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em> or </em><em>iterable of Tensors</em>) – Outputs to scale.</p>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.set_backoff_factor">
<code class="sig-name descname">set_backoff_factor</code><span class="sig-paren">(</span><em class="sig-param">new_factor</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.set_backoff_factor"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.set_backoff_factor" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>new_scale</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a>) – Value to use as the new scale backoff factor.</p>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.set_growth_factor">
<code class="sig-name descname">set_growth_factor</code><span class="sig-paren">(</span><em class="sig-param">new_factor</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.set_growth_factor"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.set_growth_factor" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>new_scale</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a>) – Value to use as the new scale growth factor.</p>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.set_growth_interval">
<code class="sig-name descname">set_growth_interval</code><span class="sig-paren">(</span><em class="sig-param">new_interval</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.set_growth_interval"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.set_growth_interval" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>new_interval</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.8)"><em>int</em></a>) – Value to use as the new growth interval.</p>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.state_dict">
<code class="sig-name descname">state_dict</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.state_dict"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.state_dict" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the state of the scaler as a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>.  It contains five entries:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">&quot;scale&quot;</span></code> - a Python float containing the current scale</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&quot;growth_factor&quot;</span></code> - a Python float containing the current growth factor</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&quot;backoff_factor&quot;</span></code> - a Python float containing the current backoff factor</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&quot;growth_interval&quot;</span></code> - a Python int containing the current growth interval</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&quot;_growth_tracker&quot;</span></code> - a Python int containing the number of recent consecutive unskipped steps.</p></li>
</ul>
<p>If this instance is not enabled, returns an empty dict.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If you wish to checkpoint the scaler’s state after a particular iteration, <a class="reference internal" href="#torch.cuda.amp.GradScaler.state_dict" title="torch.cuda.amp.GradScaler.state_dict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">state_dict()</span></code></a>
should be called after <a class="reference internal" href="#torch.cuda.amp.GradScaler.update" title="torch.cuda.amp.GradScaler.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.step">
<code class="sig-name descname">step</code><span class="sig-paren">(</span><em class="sig-param">optimizer</em>, <em class="sig-param">*args</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.step"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.step" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#torch.cuda.amp.GradScaler.step" title="torch.cuda.amp.GradScaler.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">step()</span></code></a> carries out the following two operations:</p>
<ol class="arabic simple">
<li><p>Internally invokes <code class="docutils literal notranslate"><span class="pre">unscale_(optimizer)</span></code> (unless <a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a> was explicitly called for <code class="docutils literal notranslate"><span class="pre">optimizer</span></code>
earlier in the iteration).  As part of the <a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a>, gradients are checked for infs/NaNs.</p></li>
<li><p>If no inf/NaN gradients are found, invokes <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code> using the unscaled
gradients.  Otherwise, <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code> is skipped to avoid corrupting the params.</p></li>
</ol>
<p><code class="docutils literal notranslate"><span class="pre">*args</span></code> and <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> are forwarded to <code class="docutils literal notranslate"><span class="pre">optimizer.step()</span></code>.</p>
<p>Returns the return value of <code class="docutils literal notranslate"><span class="pre">optimizer.step(*args,</span> <span class="pre">**kwargs)</span></code>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>optimizer</strong> (<a class="reference internal" href="optim.html#torch.optim.Optimizer" title="torch.optim.Optimizer"><em>torch.optim.Optimizer</em></a>) – Optimizer that applies the gradients.</p></li>
<li><p><strong>args</strong> – Any arguments.</p></li>
<li><p><strong>kwargs</strong> – Any keyword arguments.</p></li>
</ul>
</dd>
</dl>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Closure use is not currently supported.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.unscale_">
<code class="sig-name descname">unscale_</code><span class="sig-paren">(</span><em class="sig-param">optimizer</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.unscale_"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.unscale_" title="Permalink to this definition">¶</a></dt>
<dd><p>Divides (“unscales”) the optimizer’s gradient tensors by the scale factor.</p>
<p><a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a> is optional, serving cases where you need to
<a class="reference internal" href="notes/amp_examples.html#working-with-unscaled-gradients"><span class="std std-ref">modify or inspect gradients</span></a>
between the backward pass(es) and <a class="reference internal" href="#torch.cuda.amp.GradScaler.step" title="torch.cuda.amp.GradScaler.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">step()</span></code></a>.
If <a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a> is not called explicitly,  gradients will be unscaled  automatically during <a class="reference internal" href="#torch.cuda.amp.GradScaler.step" title="torch.cuda.amp.GradScaler.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">step()</span></code></a>.</p>
<p>Simple example, using <a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a> to enable clipping of unscaled gradients:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">...</span>
<span class="n">scaler</span><span class="o">.</span><span class="n">scale</span><span class="p">(</span><span class="n">loss</span><span class="p">)</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
<span class="n">scaler</span><span class="o">.</span><span class="n">unscale_</span><span class="p">(</span><span class="n">optimizer</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">clip_grad_norm_</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">(),</span> <span class="n">max_norm</span><span class="p">)</span>
<span class="n">scaler</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">optimizer</span><span class="p">)</span>
<span class="n">scaler</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
</pre></div>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>optimizer</strong> (<a class="reference internal" href="optim.html#torch.optim.Optimizer" title="torch.optim.Optimizer"><em>torch.optim.Optimizer</em></a>) – Optimizer that owns the gradients to be unscaled.</p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a> does not incur a CPU-GPU sync.</p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a> should only be called once per optimizer per <a class="reference internal" href="#torch.cuda.amp.GradScaler.step" title="torch.cuda.amp.GradScaler.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">step()</span></code></a> call,
and only after all gradients for that optimizer’s assigned parameters have been accumulated.
Calling <a class="reference internal" href="#torch.cuda.amp.GradScaler.unscale_" title="torch.cuda.amp.GradScaler.unscale_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unscale_()</span></code></a> twice for a given optimizer between each <a class="reference internal" href="#torch.cuda.amp.GradScaler.step" title="torch.cuda.amp.GradScaler.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">step()</span></code></a> triggers a RuntimeError.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="torch.cuda.amp.GradScaler.update">
<code class="sig-name descname">update</code><span class="sig-paren">(</span><em class="sig-param">new_scale=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/cuda/amp/grad_scaler.html#GradScaler.update"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.cuda.amp.GradScaler.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Updates the scale factor.</p>
<p>If any optimizer steps were skipped the scale is multiplied by <code class="docutils literal notranslate"><span class="pre">backoff_factor</span></code>
to reduce it. If <code class="docutils literal notranslate"><span class="pre">growth_interval</span></code> unskipped iterations occurred consecutively,
the scale is multiplied by <code class="docutils literal notranslate"><span class="pre">growth_factor</span></code> to increase it.</p>
<p>Passing <code class="docutils literal notranslate"><span class="pre">new_scale</span></code> sets the scale directly.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>new_scale</strong> (float or <code class="xref py py-class docutils literal notranslate"><span class="pre">torch.cuda.FloatTensor</span></code>, optional, default=None) – New scale factor.</p>
</dd>
</dl>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><a class="reference internal" href="#torch.cuda.amp.GradScaler.update" title="torch.cuda.amp.GradScaler.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a> should only be called at the end of the iteration, after <code class="docutils literal notranslate"><span class="pre">scaler.step(optimizer)</span></code> has
been invoked for all optimizers used this iteration.</p>
</div>
</dd></dl>

</dd></dl>

</div>
</div>


             </article>
             
            </div>
            <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="distributed.html" class="btn btn-neutral float-right" title="Distributed communication package - torch.distributed" accesskey="n" rel="next">Next <img src="_static/images/chevron-right-orange.svg" class="next-page"></a>
      
      
        <a href="cuda.html" class="btn btn-neutral" title="torch.cuda" accesskey="p" rel="prev"><img src="_static/images/chevron-right-orange.svg" class="previous-page"> Previous</a>
      
    </div>
  

  

    <hr>

  

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Torch Contributors.

    </p>
  </div>
    
      <div>
        Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
      </div>
     

</footer>

          </div>
        </div>

        <div class="pytorch-content-right" id="pytorch-content-right">
          <div class="pytorch-right-menu" id="pytorch-right-menu">
            <div class="pytorch-side-scroll" id="pytorch-side-scroll-right">
              <ul>
<li><a class="reference internal" href="#">Automatic Mixed Precision package - torch.cuda.amp</a><ul>
<li><a class="reference internal" href="#gradient-scaling">Gradient Scaling</a></li>
</ul>
</li>
</ul>

            </div>
          </div>
        </div>
      </section>
    </div>

  


  

     
       <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
         <script src="_static/jquery.js"></script>
         <script src="_static/underscore.js"></script>
         <script src="_static/doctools.js"></script>
         <script src="_static/language_data.js"></script>
     

  

  <script type="text/javascript" src="_static/js/vendor/popper.min.js"></script>
  <script type="text/javascript" src="_static/js/vendor/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
  <script type="text/javascript" src="_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>
 
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-90545585-1', 'auto');
  ga('send', 'pageview');

</script>

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117752657-2"></script>

<script>
  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());
  gtag('config', 'UA-117752657-2');
</script>

<img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/795629140/?label=txkmCPmdtosBENSssfsC&amp;guid=ON&amp;script=0"/>


  <!-- Begin Footer -->

  <div class="container-fluid docs-tutorials-resources" id="docs-tutorials-resources">
    <div class="container">
      <div class="row">
        <div class="col-md-4 text-center">
          <h2>Docs</h2>
          <p>Access comprehensive developer documentation for PyTorch</p>
          <a class="with-right-arrow" href="https://pytorch.org/docs/stable/index.html">View Docs</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Tutorials</h2>
          <p>Get in-depth tutorials for beginners and advanced developers</p>
          <a class="with-right-arrow" href="https://pytorch.org/tutorials">View Tutorials</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Resources</h2>
          <p>Find development resources and get your questions answered</p>
          <a class="with-right-arrow" href="https://pytorch.org/resources">View Resources</a>
        </div>
      </div>
    </div>
  </div>

  <footer class="site-footer">
    <div class="container footer-container">
      <div class="footer-logo-wrapper">
        <a href="https://pytorch.org/" class="footer-logo"></a>
      </div>

      <div class="footer-links-wrapper">
        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/">PyTorch</a></li>
            <li><a href="https://pytorch.org/get-started">Get Started</a></li>
            <li><a href="https://pytorch.org/features">Features</a></li>
            <li><a href="https://pytorch.org/ecosystem">Ecosystem</a></li>
            <li><a href="https://pytorch.org/blog/">Blog</a></li>
            <li><a href="https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md">Contributing</a></li>
          </ul>
        </div>

        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/resources">Resources</a></li>
            <li><a href="https://pytorch.org/tutorials">Tutorials</a></li>
            <li><a href="https://pytorch.org/docs/stable/index.html">Docs</a></li>
            <li><a href="https://discuss.pytorch.org" target="_blank">Discuss</a></li>
            <li><a href="https://github.com/pytorch/pytorch/issues" target="_blank">Github Issues</a></li>
            <li><a href="https://pytorch.org/assets/brand-guidelines/PyTorch-Brand-Guidelines.pdf" target="_blank">Brand Guidelines</a></li>
          </ul>
        </div>

        <div class="footer-links-col follow-us-col">
          <ul>
            <li class="list-title">Stay Connected</li>
            <li>
              <div id="mc_embed_signup">
                <form
                  action="https://twitter.us14.list-manage.com/subscribe/post?u=75419c71fe0a935e53dfa4a3f&id=91d0dccd39"
                  method="post"
                  id="mc-embedded-subscribe-form"
                  name="mc-embedded-subscribe-form"
                  class="email-subscribe-form validate"
                  target="_blank"
                  novalidate>
                  <div id="mc_embed_signup_scroll" class="email-subscribe-form-fields-wrapper">
                    <div class="mc-field-group">
                      <label for="mce-EMAIL" style="display:none;">Email Address</label>
                      <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Email Address">
                    </div>

                    <div id="mce-responses" class="clear">
                      <div class="response" id="mce-error-response" style="display:none"></div>
                      <div class="response" id="mce-success-response" style="display:none"></div>
                    </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->

                    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_75419c71fe0a935e53dfa4a3f_91d0dccd39" tabindex="-1" value=""></div>

                    <div class="clear">
                      <input type="submit" value="" name="subscribe" id="mc-embedded-subscribe" class="button email-subscribe-button">
                    </div>
                  </div>
                </form>
              </div>

            </li>
          </ul>

          <div class="footer-social-icons">
            <a href="https://www.facebook.com/pytorch" target="_blank" class="facebook"></a>
            <a href="https://twitter.com/pytorch" target="_blank" class="twitter"></a>
            <a href="https://www.youtube.com/pytorch" target="_blank" class="youtube"></a>
          </div>
        </div>
      </div>
    </div>
  </footer>

  <div class="cookie-banner-wrapper">
  <div class="container">
    <p class="gdpr-notice">To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: <a href="https://www.facebook.com/policies/cookies/">Cookies Policy</a>.</p>
    <img class="close-button" src="_static/images/pytorch-x.svg">
  </div>
</div>

  <!-- End Footer -->

  <!-- Begin Mobile Menu -->

  <div class="mobile-main-menu">
    <div class="container-fluid">
      <div class="container">
        <div class="mobile-main-menu-header-container">
          <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
          <a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
        </div>
      </div>
    </div>

    <div class="mobile-main-menu-links-container">
      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <a href="https://pytorch.org/features">Features</a>
          </li>

          <li>
            <a href="https://pytorch.org/ecosystem">Ecosystem</a>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/hub">PyTorch Hub</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <a href="https://pytorch.org/resources">Resources</a>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>
    </div>
  </div>

  <!-- End Mobile Menu -->

  <script type="text/javascript" src="_static/js/vendor/anchor.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function() {
      mobileMenu.bind();
      mobileTOC.bind();
      pytorchAnchors.bind();
      sideMenus.bind();
      scrollToAnchor.bind();
      highlightNavigation.bind();
      mainMenuDropdown.bind();
      filterTags.bind();

      // Remove any empty p tags that Sphinx adds
      $("[data-tags='null']").remove();

      // Add class to links that have code blocks, since we cannot create links in code blocks
      $("article.pytorch-article a span.pre").each(function(e) {
        $(this).closest("a").addClass("has-code");
      });
    })
  </script>
</body>
</html>